import Util from './util';

const includesSaleUriAPI = ['/to-bid-l'];

function getParams(options = []) {
    let saleUri = '';
    options.reverse().some((item) => {
        const { url = '', data } = item;
        const { query, pathname } = Util.urlParse(url);
        // 接口不包含 includesSaleUriAPI 中的任意一个
        if (!includesSaleUriAPI.some((api) => pathname.includes(api))) {
            return false;
        }

        // 提取saleUri参数
        const requestData = { ...query, ...data };
        if (requestData.saleUri) {
            saleUri = requestData.saleUri;
            return true;
        }

        return false;
    });

    return {
        from: 'h5',
        saleUri,
    };
}

export default function loginRefreshUserinfo(callback = () => {}, options) {
    WPT.getData(Util.handleApi('/app/v1.0/user/get-user-info'), getParams(options), (res) => {
        Object.assign(WPT.userinfo, res.data);
        if (!$.isEmptyObject(res.data)) {
            WPT.userinfo.userinfoSid = decodeURIComponent(res.data.sid);
        }
        callback(res);
    });
}
